package com.example.sa.myapp.handleDataClient;

import com.example.sa.myapp.sqlModel.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;


//客户端接受数据
public class handleData {
	public static int handleLogin(String un, String ps, User user) {

		String url = "http://39.108.12.42:8080/anServer/login?un=" + un + "&ps=" + ps;
		String content = JsonUtil.getWebTxt(url);
		System.out.println(content);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			if (state == 2) {
				//登录成功时传参数
				User usert = (User) JsonUtil.getObject(obj, "user");
				usert.printInfo();
				//拷贝
				user.setUser(usert);
			}
			return state;
		}
	}
	
	public static int handleRegister(User user) {
		
		String un = user.getUsername();
		String ps = user.getPassword();
		String em = user.getEmail();
		String ph = user.getPhone();
		
		String url = "http://39.108.12.42:8080/anServer/register?un=" + un + "&ps=" + ps + "&em=" + em + "&ph=" + ph;
		//System.out.println(url);
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int handleInfo(User user) {
		String un = user.getUsername();
		String ps = user.getPassword();
		String em = user.getEmail();
		String ph = user.getPhone();
		int ro = user.getRole();
		String hd = user.getHeadPortraitUrl();
		int uid = user.getId();
		//http://39.108.12.42:8080/anServer/alterInfoServlet?un=zw&ps=z&em=1231&ph=1&hd=1&ro=1&uid=80
		String url = "http://39.108.12.42:8080/anServer/alterInfoServlet?un=" + un + "&ps=" + ps + "&em=" + em + "&ph=" + ph + "&hd=" + hd + "&ro=" + ro + "&uid=" + uid;
		System.out.println(url);
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int getOrd(ArrayList<Order> ora, int uid) {
		String url = "http://39.108.12.42:8080/anServer/getOrdServlet?uid="+uid;
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			JSONArray jora = obj.getJSONArray("orders");
			for (int i = 0; i < jora.size(); i++) {
				Order or = (Order)JSONObject.toBean((JSONObject) jora.get(i), Order.class);
				ora.add(or);
			}
			return state;
		}
	}
	
	public static int getCart(ArrayList<Cart> cara, int uid) {
		String url = "http://39.108.12.42:8080/anServer/getCartServlet?uid="+uid;
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			JSONArray jora = obj.getJSONArray("carts");
			for (int i = 0; i < jora.size(); i++) {
				Cart p = (Cart)JSONObject.toBean((JSONObject) jora.get(i), Cart.class);
				cara.add(p);
			}
			return state;
		}
	}
	
	public static int addCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/inCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId()+"&anum="+ca.getAddNum();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int alterCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/alterCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId()+"&anum="+ca.getAddNum();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int dropCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/dropCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int getPro(ArrayList<Product> proa) {
		String url = "http://39.108.12.42:8080/anServer/getProServlet";
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			JSONArray jora = obj.getJSONArray("products");
			for (int i = 0; i < jora.size(); i++) {
				Product p = (Product)JSONObject.toBean((JSONObject) jora.get(i), Product.class);
				proa.add(p);
			}
			return state;
		}
	}
	
	public static int getState(JSONObject obj) {
		// 获取要转的对象数据
		int state = obj.getInt("state");
		return state;
	}
}
